package com.young.customer.service.impl;

import com.young.customer.config.UrlConstants;
import com.young.customer.domain.CustomerRegisteredDO;
import com.young.customer.domain.ThrRegisteredDO;
import com.young.customer.service.CustomerRegisteredService;
import com.young.customer.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @auter miaozhenfei
 * @date 2019/8/6 17:16
 */
@Service
public class CustomerRegisteredServiceImpl implements CustomerRegisteredService {
    public static final Logger LOGGER = LoggerFactory.getLogger(CustomerRegisteredServiceImpl.class);


    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private HttpClientUtils httpClientUtils;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public boolean Registered(Map<String, Object> mapParams) {



        // 解析map参数
        Object tempParam=null;
        tempParam = mapParams.get("userName");
        String userName = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("mobile");
        String mobile = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("password");
        String password = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("pwdStrength");
        String pwdStrength = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("stationUuid");
        String stationUuid = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("stationStoreUuid");
        String stationStoreUuid = tempParam == null ? null : tempParam.toString();


        Map returnMap= new HashMap();
        String result =null;
        if (null == userName || null == mobile || null == password ) {
            returnMap.put("return_code", ReturnCodeEnum.PARAMNULL.getKey());
            returnMap.put("message", ReturnCodeEnum.PARAMNULL.getValue());
//            return JsonUtils.objectToJson(returnMap);
            LOGGER.error("手机号注册mobile："+mobile +JsonUtils.objectToJson(returnMap) );
            return false;
        }
        String  accessToken =null;
        Object object= redisUtils.getValue("goYuongAccessToken");
        if (null==object){
            accessToken = accessTokenUtil.getaccessToken();
        }else {
            accessToken= (String) object;
        }

        //会员注册
            CustomerRegisteredDO customerRegistered= new CustomerRegisteredDO();
            customerRegistered.setAccessToken(accessToken);
            customerRegistered.setMobile(mobile);
            customerRegistered.setPassword(password);
            customerRegistered.setPwdStrength(pwdStrength);
            customerRegistered.setStationStoreUuid(stationStoreUuid);
            customerRegistered.setStationUuid(stationUuid);
            customerRegistered.setUserName(userName);
            try {
                result = httpClientUtils.doPost(UrlConstants.CUSTOMERREGISTER_URL,MapUtils.objectToMap(customerRegistered));
                LOGGER.info(String.format("mobile:%s,会员注册返回结果:%s",mobile,result ));

                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }


        return false;
    }

    /**
     * 微信注册
     * @param mapParams
     * @return
     */
    @Override
    public boolean WeCatRegistered(Map<String, Object> mapParams) {

        // 解析map参数
        Object tempParam = null;
        tempParam = mapParams.get("openid");
        String openid = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("unionid");
        String unionid = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("mobile");
        String mobile = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("nickname");
        String nickname = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("sex");
        String sex = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("headimgurl");
        String headimgurl = tempParam == null ? null : tempParam.toString();


        Map returnMap = new HashMap();
        String result = null;
        //微信注册
            if (null == openid || null == unionid || null == nickname || null == headimgurl || null == sex) {
                returnMap.put("return_code", ReturnCodeEnum.PARAMNULL.getKey());
                returnMap.put("message", ReturnCodeEnum.PARAMNULL.getValue());
//                return JsonUtils.objectToJson(returnMap);
                LOGGER.error("微信号注册unionid："+unionid +JsonUtils.objectToJson(returnMap) );
                return false;
            }
            String accessToken = null;
            Object object = redisUtils.getValue("accessToken");
            if (null == object) {
                accessToken = accessTokenUtil.getaccessToken();
            } else {
                accessToken = (String) object;
            }

            ThrRegisteredDO thrRegistered = new ThrRegisteredDO();
            thrRegistered.setAccessToken(accessToken);
            thrRegistered.setOpenid(openid);
            thrRegistered.setUnionid(unionid);
            thrRegistered.setMobile(mobile);
            thrRegistered.setNickname(nickname);
            thrRegistered.setHeadimgurl(headimgurl);
            thrRegistered.setSex(sex);

            try {
                result = httpClientUtils.doPost(UrlConstants.WEIXINREGISTERED_URL, MapUtils.objectToMap(thrRegistered));
                LOGGER.info(String.format("customerUuid:%s,微信注册返回结果:%s",unionid,result ));
                  return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        return false;
    }

    /**
     * 支付宝注册
     * @param mapParams
     * @return
     */
    @Override
    public boolean AliRegistered(Map<String, Object> mapParams) {
        // 解析map参数
        Object tempParam = null;
        tempParam = mapParams.get("userId");
        String userId = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("mobile");
        String mobile = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("nickname");
        String nickname = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("sex");
        String sex = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("headImg");
        String headImg = tempParam == null ? null : tempParam.toString();

        Map returnMap = new HashMap();
        String result = null;
        if (null == userId|| null == nickname || null == headImg || null == sex) {
            returnMap.put("return_code", ReturnCodeEnum.PARAMNULL.getKey());
            returnMap.put("message", ReturnCodeEnum.PARAMNULL.getValue());
            LOGGER.error("支付宝注册userId："+userId +JsonUtils.objectToJson(returnMap) );
            return false;
//            return JsonUtils.objectToJson(returnMap);

        }
        String accessToken = null;
        Object object = redisUtils.getValue("accessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
        } else {
            accessToken = (String) object;
        }

        ThrRegisteredDO thrRegistered = new ThrRegisteredDO();
        thrRegistered.setAccessToken(accessToken);
        thrRegistered.setOpenid(userId);
        thrRegistered.setMobile(mobile);
        thrRegistered.setNickname(nickname);
        thrRegistered.setHeadimgurl(headImg);
        thrRegistered.setSex(sex);

        try {
            result = httpClientUtils.doPost(UrlConstants.ALIREGISTERED_URL, MapUtils.objectToMap(thrRegistered));
            LOGGER.info(String.format("customerUuid:%s,微信注册返回结果:%s",userId,result ));
           return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 修改密码
     * @param mapParams
     * @return
     */
    @Override
    public boolean updateCustomerPwd(Map<String, Object> mapParams) {
        // 解析map参数
        Object tempParam = null;
        tempParam = mapParams.get("password");
        String password = tempParam == null ? null : tempParam.toString();

        tempParam = mapParams.get("mobile");
        String mobile = tempParam == null ? null : tempParam.toString();


        tempParam = mapParams.get("pwdStrength");
        String pwdStrength = tempParam == null ? null : tempParam.toString();


        Map returnMap = new HashMap();
        String result = null;
        if (null == password|| null == mobile|| null == pwdStrength) {
            returnMap.put("return_code", ReturnCodeEnum.PARAMNULL.getKey());
            returnMap.put("message", ReturnCodeEnum.PARAMNULL.getValue());
            LOGGER.error("支付宝注册userId："+mobile +JsonUtils.objectToJson(returnMap) );
            return false;
//            return JsonUtils.objectToJson(returnMap);

        }
        String accessToken = null;
        Object object = redisUtils.getValue("accessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
        } else {
            accessToken = (String) object;
        }

         mapParams.put("accessToken", accessToken);

        try {
            result = httpClientUtils.doPost(UrlConstants.UPDATECUSTOMERPWD_URL,mapParams );
            LOGGER.info(String.format("customerUuid:%s,微信注册返回结果:%s",mobile,result ));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

}
